package com.aaa.mrcai.service.impl;

import com.aaa.mrcai.mapper.GoodsMapper;
import com.aaa.mrcai.pojo.entity.*;
import com.aaa.mrcai.pojo.vo.GoodsVO;
import com.aaa.mrcai.query.GoodsQuery;
import com.aaa.mrcai.result.AjaxResult;
import com.aaa.mrcai.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author:Li-Yan-Chun
 * @Date:2021/12/6 11:37
 */
@Service
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private HttpSession session;
    @Override
    public AjaxResult list(GoodsQuery goodsQuery) {
        Map<String, Object> map = new HashMap<>();
        List<Goods> goods = goodsMapper.queryGoods(goodsQuery);
        Long aLong = goodsMapper.queryGoodsTotalCount(goodsQuery);
        map.put("currentData",goods);
        map.put("currentDataTotal",aLong);
        return AjaxResult.success(map);
    }

    @Override
    public AjaxResult listCategory() {
        List<Category> categories = goodsMapper.listCategory();
        return AjaxResult.success(categories);
    }

    @Override
    public AjaxResult delGoods(int id) {
        int i = goodsMapper.delGoods(id);
        if (i>0){
            return AjaxResult.success();
        }
        return AjaxResult.defeated();
    }

    @Override
    public AjaxResult listSpec() {
        List<Spec> specs = goodsMapper.listSpec();
        return AjaxResult.success(specs);
    }

    @Override
    public AjaxResult lisBrand() {
        List<Brand> brands = goodsMapper.listBrand();
        return AjaxResult.success(brands);
    }

    @Override
    public AjaxResult listGrade() {
        List<Grade> grades = goodsMapper.listGrade();
        return AjaxResult.success(grades);
    }

    @Override
    public AjaxResult listDeposit() {
        List<Deposit> deposits = goodsMapper.listDeposit();
        return AjaxResult.success(deposits);
    }

    @Override
    public AjaxResult addOneGoods(GoodsVO goodsVO) {
        Long goodsExist = goodsMapper.isGoodsExist(goodsVO);
        if (goodsExist>0){
            return AjaxResult.defeated("对不起，此商品已存在");
        }
        int i = goodsMapper.addOneGoods(goodsVO);
        if (i>0){
            session.setAttribute("id",goodsVO.getId());
            Integer id = (Integer) session.getAttribute("id");
            System.out.println(id+"1");
            return AjaxResult.success();
        }
        return AjaxResult.defeated();
    }

    @Override
    public AjaxResult uploadGoods(GoodsVO goodsVO) {
        Integer id = (Integer) session.getAttribute("id");
        goodsVO.setId(id);
        System.out.println(id);
        if(id == null){
            return AjaxResult.defeated("请先添加商品基本信息");
        }
        int i = goodsMapper.uploadGoods(goodsVO);
        if(i>0){
            /*session.invalidate();*/
            return AjaxResult.success();
        }
        return AjaxResult.defeated();
    }

    @Override
    public AjaxResult getOneGoods(int id) {
        GoodsVO oneGoods = goodsMapper.getOneGoods(id);
        return AjaxResult.success(oneGoods);
    }

    @Override
    public AjaxResult updateOneGoods(GoodsVO goodsVO) {
        Long goodsIdExist = goodsMapper.isGoodsIdExist(goodsVO);
        if (goodsIdExist>0){
            return AjaxResult.defeated("对不起，修改的商品已存在");
        }
        int i = goodsMapper.updateOneGoods(goodsVO);
        if (i>0){
            return AjaxResult.success();
        }
        return AjaxResult.defeated();
    }

    @Override
    public AjaxResult updateImg(GoodsVO goodsVO) {
        int i = goodsMapper.updateImg(goodsVO);
        if(i>0){
            return AjaxResult.success();
        }
        return AjaxResult.defeated();
    }
}
